package com.oraro.paygatewaybiz.channel.wxpay.tradetype.webnative;

import com.github.wxpay.sdk.WXPay;
import com.oraro.common.result.Result;
import com.oraro.paygatewaybiz.channel.wxpay.interfaces.WXPayService;
import com.oraro.paygatewaybiz.channel.wxpay.serviceImpl.AbstractWXBaseService;
import com.oraro.paygatewaybiz.constant.Constants;
import com.oraro.paygatewaybiz.exception.RRException;
import com.oraro.paygatewaybiz.module.entity.ChannelEntity;
import com.oraro.paygatewaybiz.module.entity.OrderEntity;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.Map;

/**
 * @Author: jianghuiyong
 * @Date: 2018/11/28 14:54
 * @Version 1.0
 */
@Slf4j
@Service
public class WXNativeService extends AbstractWXBaseService implements WXPayService {

    @Override
    public String type() {
        return Constants.tradeType.WXNATIVE.getType();
    }

    @Override
    public Result payOrder(OrderEntity orderEntity, ChannelEntity channelEntity) {
        WXPay wxPay = getWXPayInstance(channelEntity);
        Map<String, String> map = getWXRequestData(orderEntity);
        map.put("product_id", orderEntity.getProductId());
        Map<String, String> resp =null;
        try {
            resp = wxPay.unifiedOrder(map);
        } catch (Exception e) {
            log.error("微信统一下单失败，订单号:{},支付单号:{}", orderEntity.getOutTradeNo(), orderEntity.getOrderId());
            throw new RRException("微信统一下单失败"+ e.getMessage());
        }
        log.info("微信统一下单返回结果:{}",resp.toString());
        checkWXRsp(resp);
        return Result.success().data("code_url", resp.get("code_url"));
    }

}
